package com.lhj.xxsc.member.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 罗惠江
 * @version 1.0
 * @email 1511619294@qq.com
 * @date 2021/1/4 17:00
 */
@Configuration
public class XxscFeignConfig {
	@Bean("requestInterceptor")
	public RequestInterceptor requestInterceptor() {
		return new RequestInterceptor() {
			@Override
			public void apply(RequestTemplate requestTemplate) {
				// System.out.println("远程调用之前先执行这个拦截器");
				// 从RequestContextHoldr 拿到这个请求
				ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
				if (requestAttributes != null) {
					HttpServletRequest request = requestAttributes.getRequest();
					if (request != null) {
						String cookie = request.getHeader("Cookie");
						// 给我们发送feign 发送的请求添加上cookie
						requestTemplate.header("Cookie", cookie);
					}
				}
			}
		};
	}
}
